package cn.community.care.module.system.dal.mysql.hotproducts;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.community.care.framework.mybatis.core.mapper.BaseMapperX;
import cn.community.care.module.system.controller.admin.hotproducts.vo.HotProductsPageReqVO;
import cn.community.care.module.system.dal.dataobject.hotproducts.HotProductsDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 热门商品 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface HotProductsMapper extends BaseMapperX<HotProductsDO> {

    default List<Map<String, Object>> countTotalPriceByDayInLast7Days() {
        String startDate = LocalDateTime.now().minusDays(7).format(DateTimeFormatter.ISO_LOCAL_DATE);
        startDate = startDate + " 00:00:00";
        QueryWrapper<HotProductsDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DATE(create_time) AS date", "SUM(price) AS totalPrice")  // 选择日期和总金额
                .ge("create_time", startDate)  // 限制日期在7天之前
                .groupBy("DATE(create_time)")  // 按日期分组
                .orderByAsc("DATE(create_time)");  // 按日期升序排序

        return this.selectMaps(queryWrapper);  // 使用 selectMaps 获取结果
    }
    default PageResult<HotProductsDO> selectPage(HotProductsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<HotProductsDO>()
                .eqIfPresent(HotProductsDO::getImage, reqVO.getImage())
                .eqIfPresent(HotProductsDO::getDescription, reqVO.getDescription())
                .eqIfPresent(HotProductsDO::getIngredients, reqVO.getIngredients())
                .eqIfPresent(HotProductsDO::getUsageMethod, reqVO.getUsageMethod())
                .eqIfPresent(HotProductsDO::getSpecification, reqVO.getSpecification())
                .eqIfPresent(HotProductsDO::getPrice, reqVO.getPrice())
                .eqIfPresent(HotProductsDO::getLikes, reqVO.getLikes())
                .eqIfPresent(HotProductsDO::getComments, reqVO.getComments())
                .eqIfPresent(HotProductsDO::getFavorites, reqVO.getFavorites())
                .eqIfPresent(HotProductsDO::getProductsClassifyName, reqVO.getProductsClassifyName())
                .eqIfPresent(HotProductsDO::getProductsStatus, reqVO.getProductsStatus())
                .eqIfPresent(HotProductsDO::getSort, reqVO.getSort())
                .betweenIfPresent(HotProductsDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(HotProductsDO::getSort,HotProductsDO::getId));
    }

}